﻿'*******************************************************************
' Program: Data Center Visio Rack Tool
' Author: Albert E Edlund
' Date:
'
' Purpose:
'
'
'
'*******************************************************************


Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging

Imports System
Imports System.ComponentModel
Imports System.Collections.Generic

Imports System.Collections.ObjectModel

Imports System.Data
Imports System.IO
Imports System.Xml
Imports System.Collections

Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Visio

Imports DC_Rack.Support.clsVisioSupport
Imports DC_Rack.Objects.clsDC_RackData





Public Class clsAttrAsset


#Region " Properties and definitions "

    Public m_db As Database
    Public m_ds As DataSet
    Public m_dt As DataTable


    Public m_dc As dc_Asset
    Public m_oc As Collection(Of dc_Asset)

    Private strUserId As String

    Public m_deleteCommand As DbCommand
    Public m_insertCommand As DbCommand
    Public m_updateCommand As DbCommand

    Public m_clsVisio As DC_Rack.Support.clsVisioSupport


    Public Const readAll As String = "selectTblAttrAsset"

    ' who am I
    Private myEnum As DC_RackTables = DC_RackTables.tblAttrAsset


    Private m_visshape As Microsoft.Office.Interop.Visio.Shape
    Public Property _VisShape() As Microsoft.Office.Interop.Visio.Shape
        Get
            Return Me.m_visshape
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Shape)
            Me.m_visshape = value
        End Set
    End Property


#End Region

#Region " Move/Transform Data "

    '
    ' create columns
    '
    Public Sub CreateTableColumns(ByRef tempTable As DataTable)

        Dim newDC As dc_Asset = New dc_Asset(strUserId)

        Try

            Dim colassetID As New System.Data.DataColumn
            colassetID.ColumnName = "assetID"
            colassetID.DataType = GetType(String)
            colassetID.DefaultValue = newDC.assetId
            tempTable.Columns.Add(colassetID)

            Dim colassetVendorKey As New System.Data.DataColumn
            colassetVendorKey.ColumnName = "assetVendorKey"
            colassetVendorKey.DataType = GetType(Int32)
            colassetVendorKey.DefaultValue = newDC.assetVendorKey
            tempTable.Columns.Add(colassetVendorKey)

            Dim colassetComponentKey As New System.Data.DataColumn
            colassetComponentKey.ColumnName = "assetComponentKey"
            colassetComponentKey.DataType = GetType(Int32)
            colassetComponentKey.DefaultValue = newDC.assetComponentKey
            tempTable.Columns.Add(colassetComponentKey)

            Dim colassetVendor As New System.Data.DataColumn
            colassetVendor.ColumnName = "assetVendor"
            colassetVendor.DataType = GetType(String)
            colassetVendor.DefaultValue = newDC.assetVendor
            tempTable.Columns.Add(colassetVendor)

            Dim colassetProduct As New System.Data.DataColumn
            colassetProduct.ColumnName = "assetProduct"
            colassetProduct.DataType = GetType(String)
            colassetProduct.DefaultValue = newDC.assetProduct
            tempTable.Columns.Add(colassetProduct)

            Dim colassetModel As New System.Data.DataColumn
            colassetModel.ColumnName = "assetModel"
            colassetModel.DataType = GetType(String)
            colassetModel.DefaultValue = newDC.assetModel
            tempTable.Columns.Add(colassetModel)

            Dim colassetPartNumber As New System.Data.DataColumn
            colassetPartNumber.ColumnName = "assetPartNumber"
            colassetPartNumber.DataType = GetType(String)
            colassetPartNumber.DefaultValue = newDC.assetPartNumber
            tempTable.Columns.Add(colassetPartNumber)

            Dim colassetProductDescription As New System.Data.DataColumn
            colassetProductDescription.ColumnName = "assetProductDescription"
            colassetProductDescription.DataType = GetType(String)
            colassetProductDescription.DefaultValue = newDC.assetProductDescription
            tempTable.Columns.Add(colassetProductDescription)

            Dim colassetSerial As New System.Data.DataColumn
            colassetSerial.ColumnName = "assetSerial"
            colassetSerial.DataType = GetType(String)
            colassetSerial.DefaultValue = newDC.assetSerial
            tempTable.Columns.Add(colassetSerial)

            Dim colassetPurchaseOrder As New System.Data.DataColumn
            colassetPurchaseOrder.ColumnName = "assetPurchaseOrder"
            colassetPurchaseOrder.DataType = GetType(String)
            colassetPurchaseOrder.DefaultValue = newDC.assetPurchaseOrder
            tempTable.Columns.Add(colassetPurchaseOrder)

            Dim colassetPurchaseDate As New System.Data.DataColumn
            colassetPurchaseDate.ColumnName = "assetPurchaseDate"
            colassetPurchaseDate.DataType = GetType(Date)
            colassetPurchaseDate.DefaultValue = newDC.assetPurchaseDate
            tempTable.Columns.Add(colassetPurchaseDate)

            Dim colassetInventoryDate As New System.Data.DataColumn
            colassetInventoryDate.ColumnName = "assetInventoryDate"
            colassetInventoryDate.DataType = GetType(Date)
            colassetInventoryDate.DefaultValue = newDC.assetInventoryDate
            tempTable.Columns.Add(colassetInventoryDate)

            Dim colassetInstallDate As New System.Data.DataColumn
            colassetInstallDate.ColumnName = "assetInstallDate"
            colassetInstallDate.DataType = GetType(Date)
            colassetInstallDate.DefaultValue = newDC.assetInstallDate
            tempTable.Columns.Add(colassetInstallDate)

            Dim colassetOwner As New System.Data.DataColumn
            colassetOwner.ColumnName = "assetOwner"
            colassetOwner.DataType = GetType(String)
            colassetOwner.DefaultValue = newDC.assetOwner
            tempTable.Columns.Add(colassetOwner)

            Dim colassetCost As New System.Data.DataColumn
            colassetCost.ColumnName = "assetCost"
            colassetCost.DataType = GetType(Double)
            colassetCost.DefaultValue = newDC.assetCost
            tempTable.Columns.Add(colassetCost)

            Dim colassetWarrantyExpDate As New System.Data.DataColumn
            colassetWarrantyExpDate.ColumnName = "assetWarrantyExpDate"
            colassetWarrantyExpDate.DataType = GetType(Date)
            colassetWarrantyExpDate.DefaultValue = newDC.assetWarrantyExpDate
            tempTable.Columns.Add(colassetWarrantyExpDate)

            Dim colassetSupportContract As New System.Data.DataColumn
            colassetSupportContract.ColumnName = "assetSupportContract"
            colassetSupportContract.DataType = GetType(String)
            colassetSupportContract.DefaultValue = newDC.assetSupportContract
            tempTable.Columns.Add(colassetSupportContract)

            Dim colassetSupportContactName As New System.Data.DataColumn
            colassetSupportContactName.ColumnName = "assetSupportContactName"
            colassetSupportContactName.DataType = GetType(String)
            colassetSupportContactName.DefaultValue = newDC.assetSupportContactName
            tempTable.Columns.Add(colassetSupportContactName)

            Dim colassetSupportContactPhone As New System.Data.DataColumn
            colassetSupportContactPhone.ColumnName = "assetSupportContactPhone"
            colassetSupportContactPhone.DataType = GetType(String)
            colassetSupportContactPhone.DefaultValue = newDC.assetSupportContactPhone
            tempTable.Columns.Add(colassetSupportContactPhone)

            Dim colassetComments As New System.Data.DataColumn
            colassetComments.ColumnName = "assetComments"
            colassetComments.DataType = GetType(String)
            colassetComments.DefaultValue = newDC.assetComments
            tempTable.Columns.Add(colassetComments)

            Dim colassetLastUpdate As New System.Data.DataColumn
            colassetLastUpdate.ColumnName = "assetLastUpdate"
            colassetLastUpdate.DataType = GetType(Date)
            colassetLastUpdate.DefaultValue = newDC.assetLastUpdate
            tempTable.Columns.Add(colassetLastUpdate)

            Dim colassetLastEdit As New System.Data.DataColumn
            colassetLastEdit.ColumnName = "assetLastEdit"
            colassetLastEdit.DataType = GetType(String)
            colassetLastEdit.DefaultValue = newDC.assetLastEdit
            tempTable.Columns.Add(colassetLastEdit)

            newDC = Nothing


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        If newDC IsNot Nothing Then
            newDC = Nothing
        End If

    End Sub

    '
    ' use for writing records
    '
    Public Sub movePropertiesToDataRow _
            (ByRef tempRow As DataRow, _
             ByVal tempDC As dc_Asset)

        Try

            With tempDC
                tempRow.SetField("assetID", .assetId)
                tempRow.SetField("assetVendorKey", .assetVendorKey)
                tempRow.SetField("assetComponentKey", .assetComponentKey)
                tempRow.SetField("assetVendor", .assetVendor)
                tempRow.SetField("assetProduct", .assetProduct)
                tempRow.SetField("assetModel", .assetModel)
                tempRow.SetField("assetPartNumber", .assetPartNumber)
                tempRow.SetField("assetProductDescription", .assetProductDescription)
                tempRow.SetField("assetSerial", .assetSerial)
                tempRow.SetField("assetPurchaseOrder", .assetPurchaseOrder)
                tempRow.SetField("assetPurchaseDate", .assetPurchaseDate)
                tempRow.SetField("assetInventoryDate", .assetInventoryDate)
                tempRow.SetField("assetInstallDate", .assetInstallDate)
                tempRow.SetField("assetOwner", .assetOwner)
                tempRow.SetField("assetCost", .assetCost)
                tempRow.SetField("assetWarrantyExpDate", .assetWarrantyExpDate)
                tempRow.SetField("assetSupportContract", .assetSupportContract)
                tempRow.SetField("assetSupportContactName", .assetSupportContactName)
                tempRow.SetField("assetSupportContactPhone", .assetSupportContactPhone)
                tempRow.SetField("assetComments", .assetComments)
                tempRow.SetField("assetLastUpdate", .assetLastUpdate)
                tempRow.SetField("assetLastEdit", strUserId)
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
                (ByVal tempRow As DataRow)

        Try

            With m_dc
                .initProperties()
                .assetId = tempRow.Item("assetID")
                .assetVendorKey = tempRow.Item("assetVendorKey")
                .assetComponentKey = tempRow.Item("assetComponentKey")
                .assetVendor = tempRow.Item("assetVendor")
                .assetProduct = tempRow.Item("assetProduct")
                .assetModel = tempRow.Item("assetModel")
                .assetPartNumber = tempRow.Item("assetPartNumber")
                .assetProductDescription = tempRow.Item("assetProductDescription")
                .assetSerial = tempRow.Item("assetSerial")
                .assetPurchaseOrder = tempRow.Item("assetPurchaseOrder")
                .assetPurchaseDate = tempRow.Item("assetPurchaseDate")
                .assetInventoryDate = tempRow.Item("assetInventoryDate")
                .assetInstallDate = tempRow.Item("assetInstallDate")
                .assetOwner = tempRow.Item("assetOwner")
                .assetCost = tempRow.Item("assetCost")
                .assetWarrantyExpDate = tempRow.Item("assetWarrantyExpDate")
                .assetSupportContract = tempRow.Item("assetSupportContract")
                .assetSupportContactName = tempRow.Item("assetSupportContactName")
                .assetSupportContactPhone = tempRow.Item("assetSupportContactPhone")
                .assetComments = tempRow.Item("assetComments")
                .assetLastUpdate = tempRow.Item("assetLastUpdate")
                .assetLastEdit = tempRow.Item("assetLastEdit")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
                (ByVal tempRow As DataRow, _
                 ByRef tempDC As dc_Asset)

        Try

            With tempDC
                .initProperties()
                .assetId = tempRow.Item("assetID")
                .assetVendorKey = tempRow.Item("assetVendorKey")
                .assetComponentKey = tempRow.Item("assetComponentKey")
                .assetVendor = tempRow.Item("assetVendor")
                .assetProduct = tempRow.Item("assetProduct")
                .assetModel = tempRow.Item("assetModel")
                .assetPartNumber = tempRow.Item("assetPartNumber")
                .assetProductDescription = tempRow.Item("assetProductDescription")
                .assetSerial = tempRow.Item("assetSerial")
                .assetPurchaseOrder = tempRow.Item("assetPurchaseOrder")
                .assetPurchaseDate = tempRow.Item("assetPurchaseDate")
                .assetInventoryDate = tempRow.Item("assetInventoryDate")
                .assetInstallDate = tempRow.Item("assetInstallDate")
                .assetOwner = tempRow.Item("assetOwner")
                .assetCost = tempRow.Item("assetCost")
                .assetWarrantyExpDate = tempRow.Item("assetWarrantyExpDate")
                .assetSupportContract = tempRow.Item("assetSupportContract")
                .assetSupportContactName = tempRow.Item("assetSupportContactName")
                .assetSupportContactPhone = tempRow.Item("assetSupportContactPhone")
                .assetComments = tempRow.Item("assetComments")
                .assetLastUpdate = tempRow.Item("assetLastUpdate")
                .assetLastEdit = tempRow.Item("assetLastEdit")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    '
    '
    Public Sub moveDataTableToObservableCollection _
                (ByVal dtTemp As DataTable)

        Try

            If 0 < dtTemp.Rows.Count Then

                If m_oc IsNot Nothing Then
                    m_oc = Nothing
                    m_oc = New Collection(Of dc_Asset)
                Else
                    m_oc = New Collection(Of dc_Asset)
                End If

                Dim drTemp As DataRow
                Dim dcTemp As dc_Asset
                For Each drTemp In dtTemp.Rows
                    dcTemp = New dc_Asset
                    moveDataRowToProperties(drTemp, dcTemp)
                    m_oc.Add(dcTemp)
                Next

            End If

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub


    '
    '
    '
    Public Function moveDataTableToXML() As XmlDocument

        Dim writer = New System.IO.StringWriter
        Dim m_dt_xml As Xml.XmlDocument = Nothing

        Try


            m_dt.WriteXml(writer, XmlWriteMode.WriteSchema, True)
            m_dt_xml = Nothing
            m_dt_xml = New Xml.XmlDocument
            m_dt_xml.LoadXml(writer.ToString)

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally

            writer = Nothing

        End Try

        Return m_dt_xml


    End Function

#End Region

#Region " standard calls "


    '
    ' create a record based on property values move it to the generic datatable
    ' then copy the table to the database
    '
    Public Function addAttrAssetDataSourceRecord _
        (ByVal tempDc As dc_Asset, _
        Optional ByVal blnSilent As Boolean = False) _
        As Integer

        Dim intRowsAffected As Integer = 0

        Try

            initDataSetAndTable()

            Dim m_dr As DataRow = m_ds.Tables(dc_racktables.tblattrasset.tostring).NewRow
            movePropertiesToDataRow(m_dr, tempDc)
            m_ds.Tables(dc_racktables.tblattrasset.tostring).Rows.Add(m_dr)
            intRowsAffected = dbUpdateDataSet()


            If blnSilent = False Then
                MsgBox("DataRowsUpated = " & intRowsAffected)
            End If

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Return intRowsAffected

    End Function

    '
    '
    '
    Public Sub initDataSetAndTable()

        Try

            ' make sure the root stuff is there
            If m_ds Is Nothing Then
                m_ds = New DataSet
            Else
                m_ds = Nothing
                m_ds = New DataSet
            End If
            m_ds.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema

            If m_dt Is Nothing Then
                m_dt = New DataTable
            Else
                m_dt = Nothing
                m_dt = New DataTable
            End If

            If m_oc Is Nothing Then
                m_oc = New Collection(Of dc_Asset)
            Else
                m_oc = Nothing
                m_oc = New Collection(Of dc_Asset)
            End If

            If m_dc IsNot Nothing Then
                m_dc = Nothing
                m_dc = New dc_Asset(strUserId)
            Else
                m_dc = New dc_Asset(strUserId)
            End If

            CreateTableColumns(m_dt)
            m_dt.TableName = dc_racktables.tblattrasset.tostring
            m_ds.Tables.Add(m_dt)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub buildInsertAssetCommand()

        Try
            m_insertCommand = m_db.GetStoredProcCommand("insertAttrAsset")

            Dim cmdActive As DbCommand
            cmdActive = m_insertCommand
            m_db.AddInParameter(cmdActive, "assetId", DbType.String, "assetId", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetVendorKey", DbType.Int32, "assetVendorKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetComponentKey", DbType.Int32, "assetComponentKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetVendor", DbType.String, "assetVendor", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetProduct", DbType.String, "assetProduct", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetModel", DbType.String, "assetModel", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetPartNumber", DbType.String, "assetPartNumber", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetProductDescription", DbType.String, "assetProductDescription", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetSerial", DbType.String, "assetSerial", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetPurchaseOrder", DbType.String, "assetPurchaseOrder", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetPurchaseDate", DbType.DateTime, "assetPurchaseDate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetInventoryDate", DbType.DateTime, "assetPurchaseDate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetInstallDat", DbType.DateTime, "assetInstallDate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetOwner", DbType.String, "assetOwner", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetCost", DbType.Currency, "assetCost", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetWarrantyExpDate", DbType.DateTime, "assetWarrantyExpDate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetSupportContract", DbType.String, "assetSupportContract", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetSupportContactName", DbType.String, "assetSupportContactName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetSupportContactPhone", DbType.String, "assetSupportContactPhone", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetComments", DbType.String, "assetComments", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetLastUpdate", DbType.DateTime, "assetLastUpdate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetLastEdit", DbType.String, "assetLastEdit", DataRowVersion.Current)



        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub buildUpdateAssetCommand()

        Try
            m_updateCommand = m_db.GetStoredProcCommand("updateAttrAsset")

            Dim cmdActive As DbCommand
            cmdActive = m_updateCommand
            m_db.AddInParameter(cmdActive, "assetId", DbType.String, "assetId", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetVendorKey", DbType.Int32, "assetVendorKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetComponentKey", DbType.Int32, "assetComponentKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetVendor", DbType.String, "assetVendor", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetProduct", DbType.String, "assetProduct", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetModel", DbType.String, "assetModel", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetPartNumber", DbType.String, "assetPartNumber", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetProductDescription", DbType.String, "assetProductDescription", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetSerial", DbType.String, "assetSerial", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetPurchaseOrder", DbType.String, "assetPurchaseOrder", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetPurchaseDate", DbType.DateTime, "assetPurchaseDate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetInventoryDate", DbType.DateTime, "assetPurchaseDate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetInstallDat", DbType.DateTime, "assetInstallDate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetOwner", DbType.String, "assetOwner", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetCost", DbType.Currency, "assetCost", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetWarrantyExpDate", DbType.DateTime, "assetWarrantyExpDate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetSupportContract", DbType.String, "assetSupportContract", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetSupportContactName", DbType.String, "assetSupportContactName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetSupportContactPhone", DbType.String, "assetSupportContactPhone", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetComments", DbType.String, "assetComments", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetLastUpdate", DbType.DateTime, "assetLastUpdate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "assetLastEdit", DbType.String, "assetLastEdit", DataRowVersion.Current)



        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    '
    '
    Private Sub buildDeleteAssetCommand()



        Try
            m_deleteCommand = m_db.GetStoredProcCommand("deleteAttrAsset")

            Dim cmdActive As DbCommand
            cmdActive = m_deleteCommand
            m_db.AddInParameter(cmdActive, "assetId", DbType.String, "assetId", DataRowVersion.Current)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    ' take the existing assetTable and put it back into the database
    ' with any changes that may have occured
    '
    Public Function dbUpdateDataSet() As Int16

        Dim rowsAffected As Int16 = 0

        Try

            rowsAffected = m_db.UpdateDataSet(m_ds, dc_racktables.tblattrasset.tostring, _
                                        m_insertCommand, _
                                        m_updateCommand, _
                                        m_deleteCommand, _
                                        UpdateBehavior.Standard)
            'msgbox rowsAffected

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If

        End Try

        Return rowsAffected

    End Function


#End Region

#Region " database calls "


    '
    '
    '
    Public Sub dbSelectAllAssets()


        Try

            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand(readAll)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblattrasset.tostring)
            m_dt = m_ds.Tables(dc_racktables.tblattrasset.tostring)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    '
    '
    Public Sub dbSelectAttrAssetByCompKey(ByVal intCompKey As Int32)


        Try

            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectAttrAssetByCompKey")
            m_db.AddInParameter(cmd, "@compKey", DbType.Int32, intCompKey)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblattrasset.tostring)

            m_dt = m_ds.Tables(dc_racktables.tblattrasset.tostring)

            If m_dt.Rows.Count = 1 Then
                Dim m_dr As System.Data.DataRow = m_dt.Rows(0)
                moveDataRowToProperties(m_dr)
            End If

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch Ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(Ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally
        End Try

    End Sub



    '
    '
    '
    Public Sub dbSelectAttrAssetByRowKey _
            (ByVal lngRowKey As Int32)



        Try

            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectAttrAssetByRowKey")
            m_db.AddInParameter(cmd, "@RowKey", DbType.Int32, lngRowKey)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblattrasset.tostring)
            m_dt = m_ds.Tables(dc_racktables.tblattrasset.tostring)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch Ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(Ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Sub dbSelectAttrAssetByUrriKeys _
        (ByVal colURRIKeys As Collection(Of clsDC_RackData.rackURRI))

        initDataSetAndTable()


        Dim dtInput As DataTable = New DataTable
        Dim dtOutput As DataTable = New DataTable
        Dim drTemp As DataRow
        Dim lngRackKey As Int32
        Dim colRacks As Collection = New Collection
        Dim rckUrri As clsDC_RackData.rackURRI

        For Each rckURRI In colURRIKeys
            colRacks.Add(rckUrri.rckKey)
        Next rckURRI

        Try

            For intX = 1 To colRacks.Count
                lngRackKey = colRacks(intX)
                If Not (dtInput Is Nothing) Then
                    dtInput = Nothing
                    dtInput = New DataTable
                Else
                    dtInput = New DataTable
                End If
                dtInput = GetAttrAssetByRack(lngRackKey)
                With dtInput
                    If intX = 1 Then
                        dtOutput = Nothing
                        dtOutput = New DataTable
                        dtOutput = dtInput.Copy
                    Else
                        For Each drTemp In .Rows
                            dtOutput.ImportRow(drTemp)
                        Next
                    End If
                End With
            Next intX


            For Each drTemp In dtOutput.Rows
                m_ds.Tables(DC_RackTables.tblAttrAsset.ToString).ImportRow(drTemp)
            Next

            m_dt = m_ds.Tables(DC_RackTables.tblAttrAsset.ToString)
            m_dt.TableName = DC_RackTables.tblAttrAsset.ToString


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch Ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(Ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Function GetAttrAssetByRack _
                (ByVal lngRackKey As Int32) _
                As DataTable

        Dim m_dt1 As DataTable = New DataTable
        Dim dtReturn As DataTable = New DataTable
        Dim m_ds1 As DataSet = New DataSet

        Try

            Dim cmd1 As DbCommand
            cmd1 = m_db.GetStoredProcCommand("selectAttrAssetByRack")
            m_db.AddInParameter(cmd1, "@compKey", DbType.Int32, lngRackKey)
            m_db.LoadDataSet(cmd1, m_ds1, "temp")
            m_dt1 = m_ds1.Tables("temp")

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
            m_dt1 = Nothing
        End Try

        GetAttrAssetByRack = m_dt1

    End Function

    '
    '
    '
    Public Sub dbUpdateAssetResetComponentKey(ByVal intCompKey As Int32)


        If intCompKey = -1 Then Exit Sub

        Try

            Dim cmd As DbCommand = m_db.GetStoredProcCommand("updateAssetResetComponentKey")
            m_db.AddInParameter(cmd, "@compKey", DbType.Int32, intCompKey)

            Dim affectedRows As Int16 = m_db.ExecuteNonQuery(cmd)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub


#End Region

#Region " Visio Attributes "

    '
    ' some asset properties
    '
    Public Sub addTestAssetProperties()

        If m_dc Is Nothing Then
            m_dc = New dc_Asset
        End If

        With m_dc
            .assetId = "test123456"
            .assetVendorKey = -1
            .assetComponentKey = -1
            .assetVendor = "800lbGorilla"
            .assetProduct = "LatestOne"
            .assetModel = "BigOne"
            .assetPartNumber = "na"
            .assetSerial = "001"
        End With

    End Sub


    '
    ' pass in a shape and an asset record
    '
    Public Sub UpdateAttrAssetProperties _
            (ByRef visShape As Microsoft.Office.Interop.Visio.Shape, _
             ByVal drAsset As System.Data.DataRow)


        Try

            If Not (drAsset Is Nothing) Then
                With m_dc
                    .initProperties()
                    .assetId = drAsset.Field(Of String)("assetID")
                    .assetVendorKey = drAsset.Field(Of Integer)("assetVendorKey")
                    .assetComponentKey = drAsset.Field(Of Integer)("assetComponentKey")
                    .assetVendor = drAsset.Field(Of String)("assetVendor")
                    .assetProduct = drAsset.Field(Of String)("assetProduct")
                    .assetModel = drAsset.Field(Of String)("assetModel")
                    .assetPartNumber = drAsset.Field(Of String)("assetPartNumber")
                    .assetSerial = drAsset.Field(Of String)("assetSerial")
                End With
            Else
                addTestAssetProperties()
            End If

            With m_dc
                m_clsVisio.ForceShapeData(visShape, "assetID", .assetId, "attrAsset")
                m_clsVisio.ForceShapeData(visShape, "assetVendorKey", .assetVendorKey, "attrAsset")
                m_clsVisio.ForceShapeData(visShape, "assetComponentKey", .assetComponentKey, "attrAsset")
                m_clsVisio.ForceShapeData(visShape, "assetVendor", .assetVendor, "attrAsset")
                m_clsVisio.ForceShapeData(visShape, "assetProduct", .assetProduct, "attrAsset")
                m_clsVisio.ForceShapeData(visShape, "assetModel", .assetModel, "attrAsset")
                m_clsVisio.ForceShapeData(visShape, "assetPartNumber", .assetPartNumber, "attrAsset")
                m_clsVisio.ForceShapeData(visShape, "assetSerial", .assetSerial, "attrAsset")
            End With



        Catch com As COMException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub



#End Region

#Region " constructor "


    Public Sub New()
        Try


            m_db = DatabaseFactory.CreateDatabase()

            strUserId = Environment.UserDomainName & "/" & Environment.UserName

            '
            ' init an object class so that the default constants are available
            ' to the table definitions
            '

            If m_dc IsNot Nothing Then
                m_dc = Nothing
                m_dc = New dc_Asset(strUserId)
            Else
                m_dc = New dc_Asset(strUserId)
            End If

            initDataSetAndTable()




            m_clsVisio = New DC_Rack.Support.clsVisioSupport

            m_updateCommand = Nothing
            m_insertCommand = Nothing
            m_deleteCommand = Nothing

            buildInsertAssetCommand()
            buildDeleteAssetCommand()
            buildUpdateAssetCommand()


        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try




    End Sub


#End Region



End Class
